﻿<%@ Application Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="Portal.CDSP" %>

<script RunAt="server">
    void Application_Start(object sender, EventArgs e)
    {
    // Code that runs on application startup
    //Khởi tạo giá trị ban đầu cho biến = 0
        //HttpContext.Current.Application["Online"] = 0;
        //HttpContext.Current.Application["Truycap"] = 0;
        //Dvtien        
        Application["visitors_online"] = 0;
        Application["Total"] = 0;
        Application["Yesterday"] = 0;
    }

    void Application_End(object sender, EventArgs e)
    {
        //  Code that runs on application shutdown

    }

    void Application_Error(object sender, EventArgs e)
    {
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        HttpContext.Current.Application.Lock();
        //Dvtien

        if (Application["visitors_online"] == null) Application["visitors_online"] = 0;
        Application["visitors_online"] = int.Parse(Application["visitors_online"].ToString()) + 1;
               
        
        //if (HttpContext.Current.Application["Online"] == null) HttpContext.Current.Application["Online"] = 0;
        ////Mỗi khi có một yêu cầu truy cập khác thì tăng số người truy cập lên 1
        //HttpContext.Current.Application["Truycap"] = (int)HttpContext.Current.Application["Truycap"] + 1;
        
        //HttpContext.Current.Application["Online"] = (int)HttpContext.Current.Application["Online"] + 1;
        
        HttpContext.Current.Session.Timeout = 20;
        HttpContext.Current.Application.UnLock();      
        
        
        
        // dvtien
        try
        {
            StatisticBLL x = new StatisticBLL();
            DataTable dtb = x.TableThongKe();
            if (dtb.Rows.Count > 0)
            {
                //Application["visitors_online"] = long.Parse("0" + dtb.Rows[0]["Today"]).ToString("#,###");
                if (int.Parse(dtb.Rows[0]["Yesterday"].ToString()) == 0)
                    Application["Yesterday"] = 0;
                else
                {
                    Application["Yesterday"] = long.Parse("0" + dtb.Rows[0]["Yesterday"]).ToString("#,###");
                }                
                
                Application["ThisWeek"] = long.Parse("0" + dtb.Rows[0]["ThisWeek"]).ToString("#,###");
                //Application["PreWeek"] = long.Parse("0" + dtb.Rows[0]["PreWeek"]).ToString("#,###");
                //Application["ThisMonth"] = long.Parse("0" + dtb.Rows[0]["ThisMonth"]).ToString("#,###");
                //Application["PreMonth"] = long.Parse("0" + dtb.Rows[0]["PreMonth"]).ToString("#,###");
                Application["Total"] = long.Parse("0" + dtb.Rows[0]["Total"]).ToString("#,###");

            }
            dtb.Dispose();
            x = null;
        }
        catch 
        {
            //throw new Exception();
        } 
    }

    void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the sessionstate mode
        // is set to InProc in the Web.config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.
        Application.Lock();
        //dvtien

        if (Application["visitors_online"] == null) Application["visitors_online"] = 1;
        Application["visitors_online"] = (int)Application["visitors_online"] - 1;
        
        
        //if (Application["Online"] == null) Application["Online"] = 1;
        //Application["Online"] = (int)Application["Online"] - 1;
        
        Application.UnLock();
        
    }


    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        HttpContext myContext = HttpContext.Current;
        Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.aspx)", RegexOptions.IgnoreCase);
        try
        {
            //see if we need to rewrite the URL
            Match match_rewrite = rewrite_regex.Match(myContext.Request.Path.ToString());

            string pathRequest = myContext.Request.Path;
            //string lang = HttpContext.Current.Request["lang"];
            //if (lang == null) lang = "vie";

            if (pathRequest.IndexOf("-nd-") != -1)
            {
                int iFound = pathRequest.IndexOf("-nd-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string NewsId = pathRequest.Substring(iFound, iLastFound - iFound);
                NewsId = NewsId.Substring(4);
                //myContext.RewritePath("NewsDetail.aspx?NewsId=" + NewsId + "&lang=" + lang, false);
                myContext.RewritePath("NewsDetail.aspx?NewsId=" + NewsId, false);
            }
            //Rewrite url for update addict infomations
            if (pathRequest.IndexOf("-ud-") != -1)
            {
                int iFound = pathRequest.IndexOf("-ud-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string AddictId = pathRequest.Substring(iFound, iLastFound - iFound);
                AddictId = AddictId.Substring(4);
                //myContext.RewritePath("NewsDetail.aspx?NewsId=" + NewsId + "&lang=" + lang, false);
                myContext.RewritePath("IDIS-UpdateDrugAddict.aspx?AddictId=" + AddictId, false);
            }
            //Rewrite url for question
            if (pathRequest.IndexOf("-qa-") != -1)
            {
                int iFound = pathRequest.IndexOf("-qa-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string QId = pathRequest.Substring(iFound, iLastFound - iFound);
                QId = QId.Substring(4);
                //myContext.RewritePath("NewsDetail.aspx?NewsId=" + NewsId + "&lang=" + lang, false);
                myContext.RewritePath("Question.aspx?QId=" + QId, false);
            }
            
            if (pathRequest.IndexOf("-nd2-") != -1)
            {
                int iFound = pathRequest.IndexOf("-nd2-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string NewsId = pathRequest.Substring(iFound, iLastFound - iFound);
                NewsId = NewsId.Substring(4);
                //myContext.RewritePath("NewsDetail.aspx?NewsId=" + NewsId + "&lang=" + lang, false);
                myContext.RewritePath("NewsDetail2.aspx?NewsId=" + NewsId, false);
            }

            if (pathRequest.IndexOf("-nc-") != -1)
            {
                
                int iFound = pathRequest.IndexOf("-nc-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string GroupNewsItemId = pathRequest.Substring(iFound, iLastFound - iFound);
                GroupNewsItemId = GroupNewsItemId.Substring(4);
                string p = "0";
                int iFoundPage = GroupNewsItemId.IndexOf("-page-");
                if (iFoundPage != -1)
                {
                    string Temp = GroupNewsItemId;
                    GroupNewsItemId = GroupNewsItemId.Substring(0, iFoundPage);
                    p = Temp.Substring(iFoundPage + "-page-".Length);
                }
                myContext.RewritePath("NewsByCategory.aspx?gid=" + GroupNewsItemId + "&p=" + p, false);
                
            }
            if (pathRequest.IndexOf("-nc2-") != -1)
            {

                int iFound = pathRequest.IndexOf("-nc2-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string GroupNewsItemId = pathRequest.Substring(iFound, iLastFound - iFound);
                GroupNewsItemId = GroupNewsItemId.Substring(5);
                string p = "0";
                int iFoundPage = GroupNewsItemId.IndexOf("-page-");
                if (iFoundPage != -1)
                {
                    string Temp = GroupNewsItemId;
                    GroupNewsItemId = GroupNewsItemId.Substring(0, iFoundPage);
                    p = Temp.Substring(iFoundPage + "-page-".Length);
                }
                myContext.RewritePath("NewsByCategory2.aspx?gid=" + GroupNewsItemId + "&p=" + p, false);

            }
            if (pathRequest.IndexOf("tabId?") != -1)
            {
                int iFound = pathRequest.IndexOf("-nc2-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string GroupNewsItemId = pathRequest.Substring(iFound, iLastFound - iFound);
                GroupNewsItemId = GroupNewsItemId.Substring(5);
                int iFoundtab = pathRequest.IndexOf("tabId?");
                string tabId = pathRequest.Substring(iFoundtab, pathRequest.Length - iFoundtab);
                string p = "0";
                int iFoundPage = GroupNewsItemId.IndexOf("-page-");
                if (iFoundPage != -1)
                {
                    string Temp = GroupNewsItemId;
                    GroupNewsItemId = GroupNewsItemId.Substring(0, iFoundPage);
                    p = Temp.Substring(iFoundPage + "-page-".Length);
                }
                myContext.RewritePath("NewsByCategory2.aspx?gid=" + GroupNewsItemId + "-where?tabId=" + tabId + "&p=" + p, false);

            }
            if (pathRequest.IndexOf("-va-") != -1)
            {
                int iFound = pathRequest.IndexOf("-va-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string AlbumId = pathRequest.Substring(iFound, iLastFound - iFound);
                AlbumId = AlbumId.Substring(4);
                string p = "0";
                int iFoundPage = AlbumId.IndexOf("-page-");
                if (iFoundPage != -1)
                {
                    string Temp = AlbumId;
                    AlbumId = AlbumId.Substring(0, iFoundPage);
                    p = Temp.Substring(iFoundPage + "-page-".Length);
                }
                myContext.RewritePath("ViewAlbumDetail.aspx?AlbumId=" + AlbumId + "&p=" + p, false);
            }

            if (pathRequest.IndexOf("-bk-") != -1)
            {
                int iFound = pathRequest.IndexOf("-bk-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string AlbumId = pathRequest.Substring(iFound, iLastFound - iFound);
                AlbumId = AlbumId.Substring(4);
                string p = "0";
                int iFoundPage = AlbumId.IndexOf("-page-");
                if (iFoundPage != -1)
                {
                    string Temp = AlbumId;
                    AlbumId = AlbumId.Substring(0, iFoundPage);
                    p = Temp.Substring(iFoundPage + "-page-".Length);
                }
                myContext.RewritePath("Tapchi.aspx?AlbumId=" + AlbumId + "&p=" + p, false);
            }

            if (pathRequest.IndexOf("-vas-") != -1)
            {
                int iFound = pathRequest.IndexOf("-vas-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string AlbumId = pathRequest.Substring(iFound, iLastFound - iFound);
                AlbumId = AlbumId.Substring(5);
                string p = "0";
                int iFoundPage = AlbumId.IndexOf("-page-");
                if (iFoundPage != -1)
                {
                    string Temp = AlbumId;
                    AlbumId = AlbumId.Substring(0, iFoundPage);
                    p = Temp.Substring(iFoundPage + "-page-".Length);
                }
                myContext.RewritePath("ViewAlbumDetailSlideShow.aspx?AlbumId=" + AlbumId + "&p=" + p, false);
            }

            if (pathRequest.IndexOf("-vdoc-") != -1)
            {
                int iFound = pathRequest.IndexOf("-vdoc-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string ShortId = pathRequest.Substring(iFound, iLastFound - iFound);
                ShortId = ShortId.Substring(6);

                myContext.RewritePath("ViewDocument.aspx?DocumentId=" + ShortId, false);
            }

            if (pathRequest.IndexOf("-video-") != -1)
            {
                int iFound = pathRequest.IndexOf("-video-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string ShortId = pathRequest.Substring(iFound, iLastFound - iFound);
                ShortId = ShortId.Substring(7);
                myContext.RewritePath("VideoDetails.aspx?VideoId=" + ShortId, false);
            }

            if (pathRequest.IndexOf("-vsdoc-") != -1)
            {
                int iFound = pathRequest.IndexOf("-vsdoc-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string ShortId = pathRequest.Substring(iFound, iLastFound - iFound);
                ShortId = ShortId.Substring(7);
                myContext.RewritePath("ViewSecurityDocument.aspx?SecurityDocumentId=" + ShortId, false);
            }

            if (pathRequest.IndexOf("-vi-") != -1)
            {
                int iFound = pathRequest.IndexOf("-vi-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string ShortId = pathRequest.Substring(iFound, iLastFound - iFound);
                ShortId = ShortId.Substring(4);
                myContext.RewritePath("ViewImageItems.aspx?AdvImageId=" + ShortId, false);
            }

            if (pathRequest.IndexOf("-vfa-") != -1)
            {
                int iFound = pathRequest.IndexOf("-vfa-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string ShortId = pathRequest.Substring(iFound, iLastFound - iFound);
                ShortId = ShortId.Substring(5);
                myContext.RewritePath("ViewFixAlbum.aspx?AlbumId=" + ShortId, false);
            }


            if (pathRequest.IndexOf("-sthtml-") != -1)
            {
                int iFound = pathRequest.IndexOf("-sthtml-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string Alias = pathRequest.Substring(iFound, iLastFound - iFound);
                Alias = Alias.Substring(8);
                myContext.RewritePath("StaticHtml.aspx?Alias=" + Alias, false);
            }

            if (pathRequest.IndexOf("-sthtml2-") != -1)
            {
                int iFound = pathRequest.IndexOf("-sthtml2-");
                int iLastFound = pathRequest.LastIndexOf(".aspx");
                string Alias = pathRequest.Substring(iFound, iLastFound - iFound);
                Alias = Alias.Substring(9);
                myContext.RewritePath("StaticHtml2.aspx?Alias=" + Alias, false);
            }
        }
        catch (Exception ex) { ex.Source = ""; }
    } 
</script>

